import os.path

from flask import Blueprint
from service.file_service import FileService
from flask_jwt_extended import jwt_required

file_api = Blueprint('file_api', __name__)

"""
    @params:
        request对象 file(file):图片文件
    @returns:
        Result对象 其中包含data为保存的图片名
"""
@file_api.route('/upload_file', methods=['POST'])
@jwt_required()
def upload_file():
    return FileService.upload_file()


"""
    @params:
        file_type(str): 图片的类型，upload为刚上传的文件，result为检测结果
    @returns:
        flask.send_file 以流的形式返回
"""
@file_api.route('/download/<file_type>/<file_name>', methods=['GET'])
def download(file_type, file_name):
    return FileService.download(file_type, file_name)


"""
    @params:
        file_name:图片名称
    @returns:
        Result.success() or Result.error()
"""
@file_api.route("/delete_image", methods=['POST'])
def delete_image(file_name):
    file_path = os.path.join(".\\static\\upload", file_name)
    return FileService.delete_file(file_path)
